//
//  CustomTextView.m
//  amms
//
//  Created by jjj on 16/1/5.
//  Copyright © 2016年 YangXiao. All rights reserved.
//

#import "CustomTextView.h"
#import "UIView+extension.h"
#import "Tool.h"
@interface CustomTextView()
@property(nonatomic,weak)UILabel *placehoderLabel;
@end
@implementation CustomTextView

#pragma mark - 重写父类方法
- (void)setText:(NSString *)text {
    [super setText:text];
    [self textDidChange];
    return;
}
-(void)setAttributedText:(NSAttributedString *)attributedText{
    [super setAttributedText:attributedText];
    [self textDidChange];
}

- (void)setPlaceholder:(NSString *)placeholder {
    if (![placeholder isEqual:_placeholder]) {
        self.placehoderLabel.text=placeholder;
        self.placehoderLabel.font = [UIFont systemFontOfSize:14];
        _placeholder = placeholder;
        [self setNeedsLayout];
    }
    return;
}
- (void)setPlaceholderTextColor:(UIColor *)placeholderTextColor{
    _placeholderTextColor = placeholderTextColor;
    self.placehoderLabel.textColor=placeholderTextColor;
}
- (void)setFont:(UIFont *)font{
    [super setFont:font];
    self.placehoderLabel.font=font;
    [self setNeedsLayout];
}

#pragma mark - 父类方法
- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self configureBase];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self configureBase];
    }
    return self;
}

 

- (void)configureBase {
    UILabel *placehoderLabel  = [UILabel new];
    placehoderLabel.numberOfLines=0;
    placehoderLabel.backgroundColor=[UIColor clearColor];
    [self addSubview:placehoderLabel];
    self.placehoderLabel=placehoderLabel;
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                            selector:@selector(textDidChange)
                                name:UITextViewTextDidChangeNotification
                              object:self];
    
    self.placeholderTextColor = [UIColor colorWithRed:199.0/255.0 green:199.0/255.0 blue:205.0/255.0 alpha:1];
 
    return;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    self.placehoderLabel.y=8;
    self.placehoderLabel.x=5;
    self.placehoderLabel.width =self.width -2*self.placehoderLabel.x;
    self.placehoderLabel.height = [self.placeholder heightForFontSize:self.font.pointSize andWidth:self.placehoderLabel.width];

}

-(void)textDidChange{
    self.placehoderLabel.hidden=self.hasText;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:nil];
    return;
}

@end
